#include<iostream>

using namespace std;

void handle_1() {
    int x, y;
    double average;
    while(true) {
        cout << "Enter two number: ";
        cin >> x >> y;
        if (x == 0 || y == 0) break;
        average = 2.0 * x * y / (x + y);
        cout << "The average is " << average << endl;
    }
}

int handle_21(double scores[], int limit) {
    int i = 0;
    for (; i < limit; i++) {
        cout << "Enter #" << i + 1 << ": ";
        int score;
        cin >> score;
        if (!cin) {
            cin.clear();
            while (cin.get() != '\n') continue;
            break;
        }
        scores[i] = score;
    }
    return i;
}

void handle_22(const double scores[], int n) {
    for (int j = 0; j < n; j++) {
        cout << scores[j] << "\t";
    }
}

double handle_23(const double scores[], int n) {
    double sum = 0;
    for (int j = 0; j < n; j++) {
        sum += scores[j];
    }
    return sum / n;
}

void handle_2() {
    // 数组实际上传递的是地址
    double scores[10];
    cout << "Enter the scores(q to quit)" << endl;
    int i = handle_21(scores, 10);
    cout << "The scores are:" << endl;
    handle_22(scores, i);
    cout << endl;
    cout << "The average is " << handle_23(scores, i) << endl;
}

struct box {
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void handle_31(box b) {
    // 编写一个函数，按值传递box结构，并显示每个成员的值
    cout << "b's maker is " << b.maker << endl;
    cout << "b's height is " << b.height << endl;
    cout << "b's widht is " << b.width << endl;
    cout << "b's length is " << b.length << endl;
    cout << "b's volume is " << b.volume << endl;
    b.volume = 123;
}

void handle_32(box* b) {
    // 编写一个函数，传递box的地址
    b->volume = b->height * b->length * b->width;
}

void handle_3() {
    box b;
    cout << "Enter the box's maker: ";
    cin >> b.maker;
    cout << "Enter the box's height: ";
    cin >> b.height;
    cout << "Enter the box's width: ";
    cin >> b.width;
    cout << "Enter the box's length: ";
    cin >> b.length;
    cout << "Enter the box's volume: ";
    cin >> b.volume;
    handle_31(b);
    cout << "the volume is " << b.volume << endl;
    handle_32(&b);
    cout << "the volume is " << b.volume << endl;
}

void handle_4() {
    double ans = (5 / 47) * (1 / 27);
    cout << ans << endl;
}

int handle_5(int n) {
    if (n == 0 || n == 1) return 1;
    else return n * handle_5(n - 1);
}

int Fill_array(double list[], int size) {
    int i = 0;
    cout << "Enter number: ";
    for (; i < size; i++) {
        int num;
        cin >> num;
        if (!cin) {
            cin.clear();
            while (cin.get() != '\n') continue;
            break;
        }
        list[i] = num;
    }
    return i;
}

void Show_array(double list[], int size) {
    cout << "This array's elements are: " << endl;
    for (int i = 0; i < size; i++) {
        cout << list[i] << "\t";
    }
    cout << endl;
}

void Reverse_array(double list[], int size) {
    for (int i = 0, j = size; i < j; i++, j--) {
        double tmp = list[i];
        list[i] = list[j];
        list[j] = tmp;
    }
}

void handle_6() {
    double list[100];
    int n = Fill_array(list, 100);
    Show_array(list, n);
    Reverse_array(list, n);
    Show_array(list, n);
}

double *fill_array(double *list, double *limit) {
    double *ptr;
    double num;
    cout << "Enter number: ";
    for (ptr = list; ptr < limit;  ptr++) {
        cin >> num;
        if (!cin) {
            cin.clear();
            while (cin.get() != '\n') continue;
            break;
        }
        *ptr = num;
    }
    return ptr;
}

void show_array(const double *list, double *end) {
    cout << "This array's elements are: " << endl;
    for (const double *ptr = list; ptr < end; ptr++) {
        cout << *ptr << "\t" << endl;
    }
}

void revalue(double r, double *list, double *end) {
    for (double *ptr = list; ptr < end; ptr++) {
        *ptr *= r;
    }
}

void handle_7() {
    double list[100];
    double *size = fill_array(list, list + 100);
    show_array(list, size);
    double r;
    cin >> r;
    revalue(r, list, size);
    show_array(list, size);
}

const char* Seasons[4] = {"spring", "summer", "fall", "winter"};

void fill(double ex[]) {
    for (int i = 0; i < 4; i++) {
        cout << "Enter " << Seasons[i] << " expenses: ";
        cin >> ex[i];
    }
}

void show(double ex[]) {
    double sum = 0;
    for (int i = 0; i < 4; i++) {
        sum += ex[i];
        cout << Seasons[i] << " expenses: " << ex[i] << endl;
    }
    cout << "The sum expense is " << sum << endl;
}

void handle_8() {
    double expense[4];
    fill(expense);
    show(expense);
}

const int SLEN = 30;
struct student {
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};


int getinfo(student pa[], int n) {
    int size = 0;
    for (int i = 0; i < n; i++) {
        cout << "Enter the fullname: ";
        cin.getline(pa[i].fullname, 30);
        cout << "Enter the hobby: ";
        cin.getline(pa[i].hobby, 30);
        cout << "Enter the ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get();
        size++;
    }
    return size;
}

void display1(student st) {
    cout << "display1:" << endl;
    cout << "fullname is " << st.fullname << endl;
    cout << "hobby is " << st.hobby << endl;
    cout << "ooplevel is " << st.ooplevel << endl;
}

void display2(const student *ps) {
    cout << "display2:" << endl;
    cout << "fullname is " << ps->fullname << endl;
    cout << "hobby is " << ps->hobby << endl;
    cout << "ooplevel is " << ps->ooplevel << endl;
}

void display3(const student pa[], int n) {
    cout << "display3:" << endl;
    for (int i = 0; i < n; i++) {
        cout << "#" << i + 1 << ":" << endl;
        cout << "fullname is " << pa[i].fullname << endl;
        cout << "hobby is " << pa[i].hobby << endl;
        cout << "ooplevel is " << pa[i].ooplevel << endl;
    }
}

void handle_9() {
    cout << "Enter class size: ";
    int class_size;
    cin >> class_size;
    while (cin.get() != '\n') continue;
    student *ptr_stu = new student[class_size];
    int entered = getinfo(ptr_stu, class_size);
    for (int i = 0; i <= entered; i++) {
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
    }
    display3(ptr_stu, entered);
    delete[] ptr_stu;
}

double add(double x, double y) {
    return x + y;
}

double sub(double x, double y) {
    return x - y;
}

double calculate(double x, double y, double (*func)(double x, double y)) {
    return (*func)(x, y);
}

void handle_10() {
    while(true) {
        double x, y;
        cout << "Enter two numbers: ";
        cin >> x >> y;
        if (!cin) {
            cout << "Illegal Input, end!" << endl;
            break;
        }
        double num1 = calculate(x, y, add);
        double num2 = calculate(x, y, sub);
        cout << x << " + " << y << " = " << num1 << endl;
        cout << x << " - " << y << " = " << num2 << endl;
    }
}